/*
 * @(#)PaginationTag.java $version 2012-3-5
 * 
 * Copyright 2011 RogueDevelopment Gruop. All rights Reserved.
 * ROGUEDEVELOPMENT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nado.extensions.pagination;

import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

/**
 * @author LEESUNG
 * 
 */
public class PaginationTag extends SimpleTagSupport {
	private String type;
	private int pageSize;
	private int indexSize;
	private int page;
	private int totalRows;
	private String stepType;
	private String templateType;
	private String pageParameterName;
	private PaginationInfo paginationInfo;
	private PaginationTemplate paginationTemplate;

	public PaginationTag() {
		this.pageSize = -1;
		this.indexSize = -1;
		this.page = -1;
		this.totalRows = -1;
	}

	@Override
	public void doTag() throws JspException, IOException {
		HttpServletRequest request = (HttpServletRequest) ((PageContext) getJspContext()).getRequest();

		if (this.paginationInfo == null) {
			if (StringUtils.isNotEmpty(this.type))
				this.paginationInfo = PaginationHelper.createInfo(this.type, this.page, this.totalRows);
			else {
				this.paginationInfo = new PaginationInfo();
			}

			this.paginationInfo.setRequestURI(ObjectUtils.toString(getJspContext().getAttribute("requestURI", 2)));
			this.paginationInfo.setRequestMethod(ObjectUtils.toString(request.getMethod()));
			this.paginationInfo.setQueryString(ObjectUtils.toString(request.getQueryString()));

			String tempPageParameterName = StringUtils.defaultIfEmpty(this.pageParameterName, this.paginationInfo.getPageParameterName());
			this.paginationInfo.setPage(NumberUtils.toInt(request.getParameter(tempPageParameterName), 1));
		}

		if (StringUtils.isNotEmpty(this.pageParameterName)) {
			this.paginationInfo.setPageParameterName(this.pageParameterName);
		}
		if (this.page > 1) {
			this.paginationInfo.setPage(this.page);
		}
		if (this.pageSize > 1) {
			this.paginationInfo.setPageSize(this.pageSize);
		}
		if (this.indexSize > 1) {
			this.paginationInfo.setIndexSize(this.indexSize);
		}
		if (this.totalRows > -1) {
			this.paginationInfo.setTotalRows(this.totalRows);
		}
		if (("index".equalsIgnoreCase(this.stepType)) || ("page".equalsIgnoreCase(this.stepType))) {
			this.paginationInfo.setStepType(this.stepType);
		}
		if (StringUtils.isNotEmpty(this.templateType)) {
			this.paginationInfo.setTemplateType(this.templateType);
		}

		this.paginationTemplate = PaginationHelper.createTemplate(this.paginationInfo.getTemplateType());
		String output;
		if (getJspBody() != null)
			output = render();
		else {
			output = renderAll();
		}
		
		System.out.println("PaginationTag: \n" + output);
		getJspContext().getOut().print(output);
	}

	String render() throws JspException, IOException {
		StringWriter writer = new StringWriter();
		getJspBody().invoke(writer);
		return writer.toString();
	}

	String renderAll() throws JspException, IOException {
		PageTag[] tags = { new FirstTag(), new PrevTag(), new IndexTag(), new NextTag(), new LastTag() };
		StringBuilder builder = new StringBuilder();

		for (PageTag each : tags) {
			each.setJspContext(getJspContext());
			each.setParent(this);
			builder.append(each.render(this.paginationInfo, this.paginationTemplate));
		}

		return builder.toString();
	}

	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getIndexSize() {
		return this.indexSize;
	}

	public void setIndexSize(int indexSize) {
		this.indexSize = indexSize;
	}

	public int getPage() {
		return this.page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getTotalRows() {
		return this.totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public String getStepType() {
		return this.stepType;
	}

	public void setStepType(String stepType) {
		this.stepType = stepType;
	}

	public String getTemplateType() {
		return this.templateType;
	}

	public void setTemplateType(String templateType) {
		this.templateType = templateType;
	}

	public String getPageParameterName() {
		return this.pageParameterName;
	}

	public void setPageParameterName(String pageParameterName) {
		this.pageParameterName = pageParameterName;
	}

	public PaginationInfo getPaginationInfo() {
		return this.paginationInfo;
	}

	public void setPaginationInfo(PaginationInfo paginationInfo) {
		this.paginationInfo = paginationInfo;
	}

	public PaginationTemplate getPaginationTemplate() {
		return this.paginationTemplate;
	}

	public void setPaginationTemplate(PaginationTemplate paginationTemplate) {
		this.paginationTemplate = paginationTemplate;
	}
}
